package com.FileManager;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class File_Manager_Use {
    private static List<List<Integer>> read_sequences;
    public static void main (String[] args){
        Scanner scanner = new Scanner(System.in);
        boolean isRunning = true;
        int position = 100;
        File_Manager File = new File_Manager();

        while (isRunning) {
            displayFileMemoryMenu();
            read_sequences = readAccessingVariables("src/input/disk_scheduling.txt");
            int[] read_list = getRowValues(read_sequences,0);
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    int totalTime = File.FCFS(read_list,position);
                    System.out.println("FCFS总用时长："+totalTime);
                    break;
                case 2:
                    totalTime = File.SSTF(read_list,position);
                    System.out.println("SSTF总用时长："+totalTime);
                    break;
                case 3:
                    int[] read_list2 = {18,38,39,55,90,150,160,184};
                    totalTime = File.SCAN(read_list2,position,1);
                    System.out.println("SCAN总用时长："+totalTime);
                    break;
                case 4:
                    isRunning = false;
                    break;
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }

        scanner.close();
    }

    //    打印选择算法的菜单
    private static void displayFileMemoryMenu() {
        System.out.println("------ 主菜单 ------");
        System.out.println("文件模块管理：");
        System.out.println("1. 先来先服务调度算法（FCFS）");
        System.out.println("2. 最短寻道优先调度算法（SSTF）");
        System.out.println("3. 电梯调度算法（SCAN）");
        System.out.println("4. 退出主菜单");
    }

    //    读取AccessingVariables文件
    public static List<List<Integer>> readAccessingVariables (String fileName) {
        List<List<Integer>> sequences = new ArrayList<>();

        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String line;
            while ((line = reader.readLine()) != null) {
                List<Integer> sequence = parseLine(line);
                sequences.add(sequence);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sequences;
    }

    //    读取AccessingVariables文件后数据处理
    private static List<Integer> parseLine(String line) {
        String[] tokens = line.split(",");
        List<Integer> sequence = new ArrayList<>();
        for (String token : tokens) {
            sequence.add(Integer.parseInt(token));
        }
        return sequence;
    }

    //    从List<List<Integer>>提起一行数据
    public static int[] getRowValues(List<List<Integer>> sequences, int rowIndex) {
        List<Integer> row = sequences.get(rowIndex);
        int[] rowValues = new int[row.size()];

        for (int i = 0; i < row.size(); i++) {
            rowValues[i] = row.get(i);
        }

        return rowValues;
    }
}
